package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面类: 实现公共字段自动填充
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {}

    @Before("autoFillPointCut()")
    public void doFill(JoinPoint point) {
        log.info("公共字段自动填充开始...");
        // 获取当前被拦截的方法上的数据库操作类型
        OperationType operationType = getOperationType(point);
        // 获取到当前被拦截的方法的参数->实体对象
        Object[] args = point.getArgs();
        if (args.length == 0) {
            return;
        }
        // 为实体对象的公共字段统一赋值
        doFill(args[0], operationType);
    }

    private OperationType getOperationType(JoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
        return annotation.value();
    }

    private void doFill(Object entity, OperationType operationType){
        try {
            if (operationType == OperationType.INSERT) {
                doInsertFill(entity);
            } else if (operationType == OperationType.UPDATE) {
                doUpdateFill(entity);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void doUpdateFill(Object entity) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
        Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

        LocalDateTime dateTime = LocalDateTime.now();
        Long id = BaseContext.getCurrentId();

        setUpdateTime.invoke(entity, dateTime);
        setUpdateUser.invoke(entity, id);
    }

    private void doInsertFill(Object entity) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method setCreatedTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
        Method setCreatedUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
        Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
        Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

        LocalDateTime dateTime = LocalDateTime.now();
        Long id = BaseContext.getCurrentId();

        setCreatedTime.invoke(entity, dateTime);
        setUpdateTime.invoke(entity, dateTime);
        setCreatedUser.invoke(entity, id);
        setUpdateUser.invoke(entity, id);
    }
}
